package cn.sytton.taffecloud.gateway.config;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 处理Api访问权限配置
 *
 * @author skyrock
 */
@Component
public class ApiConfig implements InitializingBean {

    /**
     * 内置匿名api, 只允许匿名访问,登录后不能访问
     */
    private String[] anonymousArray = {
            "/auth/login/**",
            "/auth/captcha/**",
            "/auth/sms/**",
            "/code/**",
            "/doc/**",
            "/druid/**",
            "/favicon.ico"
        };

    //application.yml 配置的api
    private final Set<String> anonymous;

    public ApiConfig(ApiProperties apiProperties) {
        this.anonymous = apiProperties.getAnonymous();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        addAll();
    }

    private void addAll() {
        Set<String> anonymousSet = Arrays.stream(anonymousArray).collect(Collectors.toSet());
        anonymousSet.addAll(anonymous);
        anonymousArray = anonymousSet.toArray(new String[0]);
    }

    public String[] getAnonymousArray() {
        return anonymousArray;
    }
}
